/*
  Created on Jul 1, 2005
  (C) 2005 
  @author Michael Long, Adam D. Bradley

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

package snobject;

import snbenchlib.Util;
import snobject.MimeTypes;
import snobject.snObject;

public class snString extends snObject {

	/**
	 * instantiate a new snString with given content
	 * @param _content
	 */
	public snString(String _content) {
		super();
		
		content = _content;
	}

	public boolean equals(snObject other) {
		if(other instanceof snString)
			return getString().equals(((snString)other).getString());
		return content.equals(other.content);
	}
	
	public String getXmlMimeType() { return MimeTypes.STRING; }
	public String getWebMimeType() { return "text/plain"; }
	public String getBinaryMimeType() { return "text/plain"; }
	
	public String getString(){
		return (String)content;
	}

	public void xmlWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		if (((String)content).matches("]]>")) {
			String zstr = new String((String)content);
			out.write(zstr.replaceAll("&", "&amp;")
				  .replaceAll("<", "&lt;")
				  .replaceAll(">", "&gt;")
				  .replace('\001', '"')
				  .getBytes());
		} else {
			out.write(("<![CDATA[" +
				  ((String)content) +
				   "]]>").replace('\001', '"').getBytes());
		}
		xmlWritePost(out,0);
	}
	
	public void webWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		//find and remove binary characters
		step.Node.Indent(out, depth);
		String s = ((String)content).replace('\001', '"');
		out.write(s.getBytes());
	}

	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
		out.write(((String)content).getBytes());
	}

	public static snString xmlParse(org.w3c.dom.Element e) {
		if (!e.getTagName().equals("snobject"))
			return null;
		String z = "";
		for (org.w3c.dom.Node n = e.getFirstChild() ;
		     n != null ;
		     n = n.getNextSibling()) {
			if (n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
				org.w3c.dom.Text t = (org.w3c.dom.Text) n;
				z += t.getData();
			} else if (n.getNodeType() == org.w3c.dom.Node.ENTITY_NODE) {
				org.w3c.dom.Entity x = (org.w3c.dom.Entity) n;
				if (x.getNodeName().equals("gt")) {
					z += ">";
				} else if (x.getNodeName().equals("lt")) {
					z += "<";
				} else if (x.getNodeName().equals("amp")) {
					z += "&";
				} else {
					z += "&" + x.getNodeName() + ";";
				}
			}
		}
		return new snString(z);
	}
}
